home *** CD-ROM | disk | FTP | other *** search
- Path: gustav.unda.fi!olle
- From: olle@gustav.unda.fi (Olavi Sakari)
- Newsgroups: comp.lang.c
- Subject: Re: Splitting String ?
- Date: 12 Jan 1996 17:55:35 GMT
- Organization: Unda Oy, a Scitex company
- Distribution: world
- Message-ID: <4d67an$303@unda.fi>
- References: <HAKOLA.96Jan12151128@jung.hut.fi>
- NNTP-Posting-Host: gustav.unda.fi
-
- In article <HAKOLA.96Jan12151128@jung.hut.fi>, hakola@cadmail.hut.fi (Petri Hakola) writes:
- >
- > Have I missed something (again:)
-
- Yes...
-
- > or why doesn't this code
- > work? I should split dos-a-like-filename and add new postfix
- > instead of old one (i.e. DATA.TXT --> DATA.UPD It seems to
- > work correctly if the filename has an old postfix, but if
- > there isn't one start won't return what it should.
- >
- > - P -
- >
- >
- > ---Clip---
- > #include <stdio.h>
- > #include <string.h>
- >
- > char *newname(char *s) {
- >
- > char *dot;
- > char *start;
- > char end[5];
- >
- > strcpy(end,".UPD");
-
- Why not replace three previous lines by e.g.
-
- static char *end = ".UPD";
-
- > start = s;
- > dot = strchr(s,'.');
- > if( dot == NULL) {
- > start[strlen(start)] = '\0';
- > dot = end;
-
- Previous line not needed.
-
- > } else {
- > *dot = '\0';
- > dot++;
- > dot = end;
-
- Previous two lines not needed.
-
- > }
- > strcat(start, dot);
-
- Use
-
- strcat(start, end);
-
- instead!
-
- > return start;
- > }
- >
- > main() {
- > printf("%s\n",newname("LONGNAME.TXT"));
- > printf("%s\n",newname("NOEND"));
- > }
-
- (In the prevous code the variable start is not really needed, but wait...)
-
- You'll need to e.g. malloc() in newname for a string with enough room for the
- original string (w/o the extension) plus end (plus '\0') and copy the input
- string / head of the input string there and _then_ strcat on that!
-
- (You cannot simply strcat on "NOEND" and I don't think that it is a great idea
- to modify "LONGNAME.TXT", either, even if it just happens to work in your
- environment, and even if this is just an example of using newname!)
-
- Olle
- --
- Olavi Sakari osakari@unda.fi
- +358 0 5255 8556 (voice) +358 0 5255 8585 (fax)
- Check out http://www.unda.fi/
-